//
//  LWWelcomeViewController.swift
//  ios8-01 weibo框架搭建
//
//  Created by blue on 16/2/29.
//  Copyright © 2016年 honeywell. All rights reserved.
//

import UIKit

class LWWelcomeViewController: UIViewController {
    
    var  bottomC :NSLayoutConstraint?
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
//        let newview = UIImageView()
//        newview.image = UIImage(named: "ad_background")
//        
//        newview.sizeToFit()
//        
//        self.view = newview
//        //欢迎界面的
//        //view.backgroundColor = UIColor.orangeColor()
//        
//        //设置头像
//        newview.addSubview(self.iconImageView)
        
        self.view.addSubview(backImageView)
        self.view.addSubview(iconImageView)
        
    }
    
    override func viewWillAppear(animated: Bool) {
        
        
        
        self.prepareUI()
        
        
    }
    
    override func viewDidAppear(animated: Bool) {
        
        //给头像设置动画--UIView的动画有两个特点＝＝就近原则－旋转
     self.bottomC?.constant = -(UIScreen.mainScreen().bounds.height) - (self.bottomC?.constant)!
        
        UIView.animateWithDuration(1, delay: 0.23, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.2, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            //self.bottomC?.constant = UIScreen.mainScreen().bounds.height - (self.bottomC?.constant)!
             self.view.layoutIfNeeded()
            
            
            }) { (_ ) -> Void in
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    
                    //头像动画到指定位置，动画显示消息标签
                    self.messageLable.hidden = false

                    
                })
        }
        
        
        
    }
    
    //MARK: -给头像和消息标签添加约束
    func  prepareUI(){
        
        
        backImageView.frame = UIScreen.mainScreen().bounds
              //设置头像的frame---给头像添加约束
        iconImageView.translatesAutoresizingMaskIntoConstraints = false

        
        //设置头像的中心x
        let  x = NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
        view.addConstraint(x)
        
        //设置头像的底部
        let  bottom = NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        self.bottomC = bottom
        self.view.addConstraint(bottom)
        
        //设置头像的宽度＝高度＝＝85
        let  wide = NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85)
        self.view.addConstraint(wide)
        
        let  height = NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85)
        self.view.addConstraint(height)
        
        
        //设置消息标签的约束
        //中心点的x坐标与头像的相等
        /*
        let msg_x = NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
        self.view.addConstraint(msg_x)
        
        //顶部＝＝头像的底部
        let top_m = NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16)
        self.view.addConstraint(top_m)
        
        //高度＝＝44，宽度＝＝60
        
        let  wide1 = NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 60)
        self.view.addConstraint(wide1)
        
        let  height1 = NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 44)
        self.view.addConstraint(height1)
*/
       
    }
    
    
    //MARK: -懒加载头像和消息标签
    lazy  var  iconImageView : UIImageView = {
        
       let  icon = UIImageView()
        
        icon.image = UIImage(named: "avatar_default_big")
        
        icon.sizeToFit()
        
        icon.layer.cornerRadius = 42.5
        
        return  icon
    }()
    
    lazy  var  messageLable :UILabel = {
        
        let  messageLab = UILabel()
        
        messageLab.text = "欢迎回来"
        //messageLab.sizeToFit()
        messageLab.textColor = UIColor.orangeColor()
        
        
        return  messageLab
    }()
    
    lazy  var  backImageView :UIImageView = {
        
        let  imageView = UIImageView()
        
        imageView.image = UIImage(named: "ad_background")
        imageView.sizeToFit()
        
        return  imageView
        
    }()

}
